package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import www.lagou.entity.Phone;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * 电话类的数据操作
 */
public class PhoneDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    /**
     * 根据价格和生产日期查询手机
     * @param price 价格
     * @param prodate 生产日期
     * @return 查询到的手机
     * @throws SQLException
     */
    public List<Phone> findPhoneByPriceAndDate(double price, String prodate) throws SQLException {
        String sql = "select * from phone where price > ? and prodate < ?";
        Object[] objects = {price,prodate};
        List<Phone> phones = queryRunner.query(sql, new BeanListHandler<Phone>(Phone.class), objects);
        if (phones != null || phones.size() != 0)
            return phones;
        else
            return null;
    }

    /**
     * 根据手机颜色查询手机
     * @param color 颜色
     * @return 查询到的手机
     * @throws SQLException
     */
    public List<Phone> findPhoneByColor(String color) throws SQLException {
        String sql = "select * from phone where color = ?";
        List<Phone> phones = queryRunner.query(sql, new BeanListHandler<Phone>(Phone.class), color);
        if (phones != null || phones.size() != 0)
            return phones;
        else
            return null;
    }
}
